<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;

// 竞彩 推荐 类
class RecommendController extends Controller
{
    
	
	// 推荐赛事 列表
	public function recommendIndex( $status='2', Request $request )
	{
        // 开始
        // 获取路由
        $route_array = request()->route()->getAction();
        $controller = explode("\\",$route_array['controller']);
        $result = explode("@",$controller[3]);
        // 控制器 	$result[0];
        // 方法 	$result[1];
        $auth_final = '';
        foreach( Session::get('auths') as $key=> $auth_info )
        {
            // 符合条件
            if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
            {
                $auth_final = true;
                break;
            }else
            {
                $auth_final = false;
            }
        }
        // 权限验证失败
        if( !$auth_final )
        {
            return redirect('/')->with( ['authinfo'=>'没有此项权限'] );
        }
        // 结尾
		// 获取搜索内容
		$keyword = $request -> input('keyword',"");
		if( empty($keyword) )
		{
			// 为空 说明没有搜索
			$one = 'r.id';
			$two = '!=';
			$three = '0';
		}else{
			$one = 'r.home';
			$two = 'like';
			$three = '%'.$keyword.'%';
			
		}
		// 查询所有资讯 每页20条数据
		$recommends = DB::table('new_recommend as r')
					-> select( 'r.id','r.home','r.away','r.create_at',DB::raw("case when r.status=1 then '草稿' when r.status=2 then '待审核' when r.status=3 then '审核通过'  when r.status=4 then '已推荐' when r.status=5 then '头条'  when r.status=6 then '未通过' end as statusInfo"),'r.status','u.name as author',DB::raw("case when r.judge=0 then '' when r.judge=1 then '失' when r.judge=2 then '中' end as judgeInfo"),'r.judge','r.final_home','r.final_away' )
					-> leftJoin('user as u','u.id','=','r.user_id')
					-> where( $one,$two,$three ) 
					// 1草稿 2待审核 3审核通过 4推荐 5头条 6未通过
					-> where( 'r.status','=',$status )
					-> orderBy('r.create_at','desc')
					-> paginate(20);
					
		return view( 'recommend.recommend',[ 'recommends'=>$recommends,'request'=>$keyword,'status'=>$status ] );
	
	}
	
	
	
	// 最终比分
    public function finalScore( $recommend_id='' , Request $request )
    {
    	$data = $request -> except('_token');
		$update = array();
		if( $data['who']=='home' )
		{
			$update['final_home'] = $data['final_score'];
		}else
		{
			$update['final_away'] = $data['final_score'];
		}
		
		$res = DB::table('new_recommend')
			-> where('id',$recommend_id)
			-> update( $update );
		if( $res )
		{
			$this -> show_msg('成功');
		}
		
		$this -> show_msg('失败');
		
	}
	
	
	
	// 批量 删除 赛事推荐
	public function delRecommendAll( Request $request  )
	{
		// 包含 赛事推荐 id
		$data = $request -> except('_token');
		// 清除无效值
		unset($data['all'][0]);		
		foreach( $data['all'] as $key=> $recommend_id )
		{
			$pics = DB::table('new_recommend_pic')
					-> where( 'new_recommend_id','=',$recommend_id )
					-> get();
			if( count($pics)>0 )
			{
				foreach( $pics as $key => $pic_info )
				{
					// 线上
					$file = "../../image".$pic_info -> pic_url;
					// 删除 文件
					if( file_exists($file) )
					{
						unlink($file);
					}
				}
				DB::table('new_recommend_pic')
						-> where( 'new_recommend_id','=',$recommend_id )
						-> delete();
			
			}
			
		}
		// 2.主表
		$res = DB::table('new_recommend')
				-> whereIn( 'id',$data['all'] )
				-> delete();
		if( !$res )
		{
			$this -> show_msg('删除失败');
		}
		$this -> show_msg('删除成功');
	}
	
	
	
	
	// 批量 操作 竞彩推荐 审核通过
	public function passAllrecommend( Request $request )
	{
		// 包含 竞彩推荐 id
		$data = $request -> except('_token');
		// 清除无效值
		unset($data['all'][0]);	
		
		foreach( $data['all'] as $key=>$recommend_id )
		{
			// 获取现有积分
			$recommend_score  = DB::table('new_recommend')
				-> select('score','user_id')
				-> where('id',$recommend_id)
				-> first();
			// 赛事还未加过积分
			if( $recommend_score->score < 5 )
			{
				// 获取用户现有积分
				$userscore = DB::table('user')
						-> select('score')
						-> where('id',$recommend_score->user_id)
						-> first();
				$final = $userscore->score + 5;
				// 更改用户积分
				$score  = DB::table('user')
					-> where('id',$recommend_score->user_id)
					-> update( ['score'=>$final] );
				// 1.主表
				DB::table('new_recommend')
					-> where( 'id',$recommend_id )
					// 3审核通过
					-> update( ['status'=>3,'score'=>5] );
			}else
			{
				
				DB::table('new_recommend')
					-> where( 'id',$recommend_id )
					// 3审核通过
					-> update( ['status'=>3] );
			
			}
			
		
		}
		
		$this -> show_msg('操作成功');
	
	}
	
	
	
	// 执行 修改赛事推荐 状态
	public function uprecommendStatus( $recommend_id,Request $request )
	{
		// 包含状态
		$data = $request -> except('_token');
		// 3审核通过 4推荐 5头条
		if( $data['status']=='3' || $data['status']=='4' || $data['status']=='5' )
		{
			$recommend_score  = DB::table('new_recommend')
					-> select('score','user_id')
					-> where('id',$recommend_id)
					-> first();
			// 赛事还未加过积分
			if( $recommend_score->score < 5 )
			{
				$data['score']=5;
				// 获取用户现有积分
				$userscore = DB::table('user')
						-> select('score')
						-> where('id',$recommend_score->user_id)
						-> first();
				$final = $userscore->score + 5;
				// 更改用户积分
				DB::table('user')
					-> where('id',$recommend_score->user_id)
					-> update( ['score'=>$final] );
			}
		}
        $res  = DB::table('new_recommend')
					-> where('id',$recommend_id)
					-> update( $data );
		if($res)
		{
			// 修改成功
			$this -> show_msg('操作成功');
		}else
		{
			$this -> show_msg('操作失败');
		}
	}
	
	
	
	// 评判赛事推荐结果 中 失
	public function uprecommendJudge( $recommend_id,Request $request )
	{
		// 包含 评判结果
		$data = $request -> except('_token');
        $res  = DB::table('new_recommend')
					-> where('id',$recommend_id)
					-> update( $data );
		if($res)
		{
			// 修改成功
			$this -> show_msg('操作成功');
		}else
		{
			$this -> show_msg('操作失败');
		}
	}
	
	
	// 赛事推荐
	public function recommendpreview( $recommend_id='' )
	{
        // 开始
        // 获取路由
        $route_array = request()->route()->getAction();
        $controller = explode("\\",$route_array['controller']);
        $result = explode("@",$controller[3]);
        // 控制器 	$result[0];
        // 方法 	$result[1];
        $auth_final = '';
        foreach( Session::get('auths') as $key=> $auth_info )
        {
            // 符合条件
            if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
            {
                $auth_final = true;
                break;
            }else
            {
                $auth_final = false;
            }
        }
        // 权限验证失败
        if( !$auth_final )
        {
            return redirect('/')->with( ['authinfo'=>'没有此项权限'] );
        }
        // 结尾
		$recommend = DB::table('new_recommend')
						-> select('*',DB::raw("replace(home_pic,'/images','".config('app.imgPath')."/images' ) as home_pic "),DB::raw("replace(away_pic,'/images','".config('app.imgPath')."/images' ) as away_pic"),DB::raw("replace(content,'/images','".config('app.imgPath')."/images' ) as content") )
						-> where('id', $recommend_id)
						-> first();
		
		return view( 'recommend.recompreview', [ 'recommend'=>$recommend,'title' => '赛事推荐预览' ] );
	
	}
	
	
	
	
	

}
